package com.example.demo.controller;

import com.example.demo.mapper.UserInfoMapper;
import com.example.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {@Autowired
private DataSourceTransactionManager dataSourceTransactionManager;
    // 定义事务属性
    @Autowired
    private TransactionDefinition transactionDefinition;
    @Autowired
    private UserService userService;

    @Transactional(rollbackFor = {Exception.class,Error.class},isolation = Isolation.READ_COMMITTED)
    @RequestMapping("login")
    public String insert(String username,String password) throws IOException {
        userService.insert(username,password);
        try {
            int a= 10/0;
        }
    catch (Exception e){
            log.info("把异常向上抛出");
            throw new IOException();
    }

        log.info("用户登录成功");
        return "用户登录成功";
    }

}
